package org.mendoza.salud.hvt.client.local.gui.persona;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.user.client.ui.*;
import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.common.client.api.RemoteCallback;
import org.jboss.errai.ui.shared.api.annotations.DataField;
import org.jboss.errai.ui.shared.api.annotations.EventHandler;
import org.jboss.errai.ui.shared.api.annotations.Templated;
import org.mendoza.salud.hvt.client.shared.model.global.Pais;
import org.mendoza.salud.hvt.client.shared.model.global.TipoDeDocumento;
import org.mendoza.salud.hvt.client.shared.model.services.GeoServiceRemote;
import org.mendoza.salud.hvt.client.shared.model.services.PersonaServiceRemote;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;

@Templated("TipoDeDocumentoView.html#editor")
public class TipoDeDocumentoView extends Composite {
    @Inject
    Caller<PersonaServiceRemote> personaServiceRemoteCaller;

    @Inject
    Caller<GeoServiceRemote> geoServiceRemoteCaller;

    @Inject
    @DataField("pais")
    SuggestBox paisSuggestBox;
    @Inject
    @DataField
    private TextBox descripcion;
    @Inject
    @DataField("sigla")
    private TextBox sigla;
    @Inject
    @DataField("ok")
    private Button ok;

    private HashMap<String,Pais> paises;

    @PostConstruct
    public void init() {
        paisSuggestBox.setAutoSelectEnabled(true);
    }


    @EventHandler("descripcion")
    public void evaluateDescription(KeyUpEvent event) {
        String[] palabras = descripcion.getValue().split(" ");
        String siglaTemp = "";
        for (String s : palabras) {
            if (!s.equals("de"))
                siglaTemp = siglaTemp + s.substring(0, 1);
        }
        sigla.setText(siglaTemp);
    }

    @EventHandler("ok")
    public void editorOk(ClickEvent event) {
        TipoDeDocumento tipoDeDocumento = new TipoDeDocumento();
        tipoDeDocumento.setPais(paises.get(paisSuggestBox.getValue()));
        tipoDeDocumento.setDescripcion(descripcion.getValue());
        tipoDeDocumento.setSigla(sigla.getValue());
        personaServiceRemoteCaller.call().persistTipoDeDocumento(tipoDeDocumento);
    }

    @EventHandler("pais")
    public void findPais(KeyUpEvent event) {
        geoServiceRemoteCaller.call(new RemoteCallback<List<Pais>>() {
            @Override
            public void callback(List<Pais> paisList) {
                MultiWordSuggestOracle paisesMultiWordSuggestOracle = (MultiWordSuggestOracle) paisSuggestBox.getSuggestOracle();
                paisesMultiWordSuggestOracle.clear();
                if (paisSuggestBox.getText().length() > 1) {
                    paises = new HashMap<String, Pais>();
                    for (Pais p : paisList) {
                        paisesMultiWordSuggestOracle.add(p.getNombre());
                        paises.put(p.getNombre(),p);
                    }
                }
            }
        }).getPaisesByNombre(paisSuggestBox.getValue());
    }

}
